package com.hanson.controller;

import com.hanson.annotation.Log;
import com.hanson.annotation.RateLimiter;
import com.hanson.annotation.WebLogIgnore;
import com.hanson.common.api.PageResult;
import com.hanson.common.api.ResultCode;
import com.hanson.rate_limit.RateStrategy;
import com.hanson.service.WebLogService;
import com.hanson.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * @CLassName WebLogController
 * @Description 接口操作日志控制器
 * @Author li hao xin
 * @Date 2020/12/1 10:48
 **/
@Api(tags = "接口操作日志控制器")
@Slf4j
@Controller
@Log
@RequestMapping("/webLog")
public class WebLogController {

    @Autowired
    private WebLogService webLogService;

    //打开管理员列表
    @ApiOperation("打开操作日志列表")
    @GetMapping("/lists")
    @WebLogIgnore
    public String webLogLists(){
        return "web/weblog/weblog-list";
    }


    @ApiOperation("接口操作日志列表分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit",value = "分页大小",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "page",value = "当前页码",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "search",value = "搜索关键字",required = false,dataType = "String"),
            @ApiImplicitParam(name = "startTime",value = "搜索日志开始时间",required = false,dataType = "Date"),
            @ApiImplicitParam(name = "endTime",value = "搜索日志结束时间",required = false,dataType = "Date")
    })
    @WebLogIgnore
    @RateLimiter(time = 5,count = 30,rateStrategy = RateStrategy.IP)
    @PostMapping("/lists")
    @ResponseBody
    public PageResult webLogListPage(PageUtil pageUtil, String search, Date startTime, Date endTime){
        //如果搜索字符串过长直接返回,校验时间区间不超过一年
        PageResult pageResult = PageResult.pageSuccessResult();
        if ((StringUtils.isNotBlank(search) && search.length() > 20) | ((startTime != null && endTime != null) && (endTime.before(startTime) | diffDay(startTime,endTime) > 360))
        | ((startTime != null && endTime == null) && diffDay(startTime,new Date()) > 360)){
            String msg = "";
            if ((StringUtils.isNotBlank(search) && search.length() > 20)){
//                System.out.println("search key is null");
                msg = "search key is null";
            }else if (((startTime != null && endTime != null) && (endTime.before(startTime) |diffDay(startTime,endTime) > 360))){
//                System.out.println("between more than one year");
                msg = "between more than one year";
            }else if (((startTime != null && endTime == null) && diffDay(startTime,new Date()) > 360)){
//                System.out.println("start with more than one year");
                msg = "start with more than one year";
            }else{
                msg = ResultCode.VALIDATE_FAILED.getContent();
            }
            pageResult.setCount(0);
            pageResult.setData(null);
            pageResult.setCode(ResultCode.VALIDATE_FAILED.getCode());
            pageResult.setContent(msg);
            return pageResult;
        }
        pageUtil.setSizeAndStart();
        webLogService.webLogSplit(pageUtil,search,startTime,endTime,pageResult);
        return pageResult;
    }

    /**
    *
    * 获取两个日期之间的天数，不足一天返回0
    *
    * @params: [start, end]
    * @param: start				开始日期
    * @param: end               截止日期
    * @return: {@link int}
    * @since: 2021/1/6
    **/
    private int diffDay(Date start,Date end){
        return (int)((end.getTime() - start.getTime())/(1000 * 3600 * 24));
    }
}
